<?php
declare(strict_types=1);
namespace App\Dao;

use App\Model\WxserviceType;

class WxserviceTypeDao
{

    /**
     * 根据id获取数据
     * @param array $select 筛选数组
     * @return array
     */
    public function getList($select=['*']){
        return WxserviceType::query()->where(['is_del'=>1])->select($select)->get()->toArray();
    }

    /**
     * 根据id数组获取数据
     * @param array $ids id数组
     * @param array $select 筛选数组
     * @return array
     */
    public function getListByIds($ids,$select=['*']){
        return WxserviceType::query()->whereIn('id',$ids)->select($select)->get()->toArray();
    }

    /**
     * 获取列表数据
     * @param array $where
     * @param string[] $columns
     * @param int $pageSize
     * @param null $page
     * @return array
     */
    public function getListPage($where=[], $page=null, $pageSize=15,  $columns=['*']){
        $data = WxserviceType::query()->where($where)->orderBy('id', 'desc')->paginate(intval($pageSize), $columns,'page', $page)->toArray();
        $count = WxserviceType::query()->where($where)->count();
        return [
            'list' => @$data['data'],
            'count' => $count
        ];
    }

    /**
     * 查询全部
     * @param string[] $columns
     * @return array
     */
    public function getAll($columns=['*']){
        return WxserviceType::query()->leftJoin('wxservice_ope', 'wxservice_type.wxservice_ope_id', '=', 'wxservice_ope.id')
            ->where([
                'wxservice_type.is_del' => 1,

            ])->orderBy('wxservice_type.id', 'desc')->get($columns)->toArray();
    }


    /**
     * 新建
     * @param $insertArray
     * @return int
     */
    public function add($insertArray){
        $insertArray['add_time'] = time();
        return WxserviceType::query()->insertGetId($insertArray);

    }

    /**
     * 根据id获取信息
     * @param $id
     * @param string[] $columns
     * @param bool $isDel
     * @return array
     */
    public function findById($id, $columns=['*'], $isDel=true){

        if($isDel){
            return WxserviceType::query()->where(['id'=>$id,'is_del'=>1])->first($columns);
        }

        return WxserviceType::query()->find($id, $columns);
    }

    /**
     * 根据id更新信息
     * @param $id
     * @param $updateArray
     * @return int
     */
    public function updateById($id, $updateArray){
        return WxserviceType::query()->where('id',  $id)->update($updateArray);
    }

    /**
     * 更新信息
     * @param $where
     * @param $updateArray
     * @return int
     */
    public function updateByWhere($where, $updateArray, $titleArray = []){
        if(empty($titleArray)){
            return WxserviceType::query()->where($where)->update($updateArray);
        }else{
            return WxserviceType::query()->where($where)->whereIn('title', $titleArray)->update($updateArray);
        }

    }


    /**
     * 查询全部
     * @param $where
     * @param string[] $columns
     * @return array
     */
    public function getAllByWhere($where, $columns=['*']){
        return WxserviceType::query()->where($where)->orderBy('id', 'asc')->get($columns)->toArray();
    }


    /**
     * 获取信息
     * @param $id
     * @param string[] $columns
     * @param bool $isDel
     * @return array
     */
    public function findByWhere($where, $columns=['*']){

        return WxserviceType::query()->where($where)->first($columns);
    }

}